package com.huike.service;

import com.huike.dao.SysCustomDAO;
import com.huike.po.SystemCustom;
import com.huike.pojo.BatchSendSysMsg;
import com.huike.pojo.SystemCustomObject;
import com.huike.websocket.ChatWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysCustomServiceImpl implements SysCustomService {
    @Autowired
    SysCustomDAO sysCustomDAO;
    @Autowired
    private SystemCustom systemCustom;
    @Autowired
    private ChatWebSocketHandler webSocket;
    @Autowired
    private SystemCustomObject systemCustomObject;
    @Override
    public boolean markSysCustomIsRead(String account) {
        sysCustomDAO.updateAllSysCustomToHaveRead(account);
        return true;
    }

    @Override
    public boolean deleteSysCustom(String account) {
        sysCustomDAO.deleteAllSysCustom(account);
        return true;
    }

    @Override
    public boolean batchSendSysMsg(BatchSendSysMsg msg) {
        // 插入系统通知
        List<String> accountList =msg.getAccountList();
        String content = msg.getTitle()+';'+msg.getContent();
        for(String account: accountList){
            systemCustom.setSctype_id(12);
            systemCustom.setSyscustom_from(msg.getAccountFrom());
            systemCustom.setSyscustom_to(account);
            systemCustom.setSyscustom_content(content);
            sysCustomDAO.insertCustomSys(systemCustom);
        }

        if(msg.getAccountList().size()>0){
            //设置websocket传输实体的内容
            systemCustomObject.setSysCustomType("CustomSystem");
            systemCustomObject.setSysCustomFrom(msg.getAccountFrom());
            systemCustomObject.setSysCustomContent(content);
            systemCustomObject.setSysCustomTime(msg.getTime());
            //调用websocket
            webSocket.sendCustomSysToUsers(systemCustomObject, msg.getAccountList());
        }

        return false;
    }
}
